package mapkit.layers
{
	import mapkit.MapLayer;

	public class WFSLayer extends MapLayer
	{
		private var _baseurl:String="";

		public function get baseURL():String
		{
			return _baseurl;
		}

		public function set baseURL(value:String):void
		{
			_baseurl=value;
		}

		private var _typename:String="";

		public function get typeName():String
		{
			return _typename;
		}

		public function set typeName(value:String):void
		{
			_typename=value;
		}

		private var _service:HTTPService=null;

		override public function load():void
		{
			super.load();
			_service=new HTTPService();
			_service.addEventListener(ResultEvent.RESULT, resultHandler);
		}

		override public function unload():void
		{
			_service.removeEventListener(ResultEvent.RESULT, resultHandler);
			_service=null;
			super.unload();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			if (baseURL && map)
			{
				_service.url=baseURL + "request=getfeature&srs=" + map.extent.srid +
					"&bbox=" + map.extent.minX + "," + map.extent.minY + "," + map.extent.maxX +
					"," + map.extent.maxY + "&width=" + map.width + "&height=" +
					map.height;
				_service.send();
			}
		}

		protected function renderFeatures():void
		{

		}

		private function parseFeatures(event:ResultEvent):void
		{
			var fcollection:Object=event.result["FeatureCollection"]["featureMembers"];
			for (var key:String in fcollection)
			{
				if (fcollection[key] is ArrayCollection)
				{
					_typename=key;
					//TODO fcollection[key]
					break;
				}
			}
		}

	}
}